package com.itheima.service.store.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.QuestionDao;
import com.itheima.domain.store.Question;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.QuestionService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class QuestionServiceImpl implements QuestionService {

    @Override
    public String save(Question question,boolean flag) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionDao questionDao=MapperFactory.getMapper(sqlSession,QuestionDao.class);
            String id= UUID.randomUUID().toString();
            question.setId(id);
            //设置审核状态为 0 为审核中
            question.setReviewStatus("0");
            question.setCreateTime(new Date());

             if(flag) {
              //设置图片存储名称为ID
              question.setPicture(id);

             }
            questionDao.save(question);
            TransactionUtil.commit(sqlSession);

            return id;
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(Question question) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionDao questionDao=MapperFactory.getMapper(sqlSession,QuestionDao.class);
            questionDao.delete(question);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Question question,boolean flag) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionDao questionDao=MapperFactory.getMapper(sqlSession,QuestionDao.class);


            if(flag) {
                //设置图片存储名称为ID
                question.setPicture(question.getId());

            }

            questionDao.update(question);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Question findById(String id) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionDao questionDao=MapperFactory.getMapper(sqlSession,QuestionDao.class);
            return questionDao.findById(id);
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Question> findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionDao questionDao=MapperFactory.getMapper(sqlSession,QuestionDao.class);
            return questionDao.findAll();
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo<Question> findAll(int page, int size) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionDao questionDao=MapperFactory.getMapper(sqlSession,QuestionDao.class);
            PageHelper.startPage(page,size);
            List<Question>all=questionDao.findAll();
            PageInfo pageInfo=new PageInfo(all);
            return pageInfo;
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
